from tkinter import *
import tkinter
import time
import threading
import random
import openpyxl
from PIL import ImageTk, Image,ImageDraw, ImageFont
from openpyxl.styles import Font



def getPeopleList():
    workbook = openpyxl.load_workbook("./peopleList.xlsx", data_only=True)
    list = []
    list_2 =[]
    sheet = workbook[workbook.sheetnames[0]]
    for row in range(2, sheet.max_row + 1):
        if sheet.cell(row=row, column=2).value == "" or sheet.cell(row=row, column=3).value == "":
            continue
        else:
            list.append(sheet.cell(row=row, column=2).value)
            list_2.append(sheet.cell(row=row, column=3).value)
            names = [list + '_' + str(id_) for list, id_ in zip(list, list_2)]

    return list,list_2,names

namelist,ID,total_na = getPeopleList()

class Choujiang:
#初始化方法
    def __init__(self):
        #准备好界面
        self.root = tkinter.Tk()
        self.root.title('中秋节抽奖LuckyDraw系统')
        self.root.geometry('1280x1200')  # 定义界面大小
        self.root.resizable(False, False)
        # self.root.minsize(600, 600)
        # 添加背景图片
        self.canvas = tkinter.Canvas(self.root,
                                     width=1280,  # 指定Canvas组件的宽度
                                     height=1810,  # 指定Canvas组件的高度
                                     bg='white')  # 指定Canvas组件的背景色
        self.image = Image.open("3.jpg")
        self.im = ImageTk.PhotoImage(self.image)
        self.canvas.create_image(780, 450, image=self.im)  # 使用create_image将图片添加到Canvas组件中
        self.canvas.pack()

        # 声明一个是否按下开始的变量
        self.isloop = False
        self.newloop = False
        #调用设置界面的方法
        self.setwindow()
        self.root.mainloop()


    # 建立一个新线程的函数
    def set_first(self):
        if self.isloop == False:
            # 建立线程
            t = threading.Thread(target=self.rounds)
            # 开启线程运行
            t.start()
            # 设置循环开始标志ask(self):nknewtas
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True

    def set_second(self):
        if self.isloop == False:
            # 建立线程
            t = threading.Thread(target=self.rounds_2)
            # 开启线程运行
            t.start()
            # 设置循环开始标志ask(self):nknewtas
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True

    def set_third(self):
        if self.isloop == False:
            # 建立线程
            t = threading.Thread(target=self.rounds_3)
            # 开启线程运行
            t.start()
            # 设置循环开始标志ask(self):nknewtas
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True

    #界面布局方法
    def setwindow(self):
        #开始停止按钮
        self.btn_start = tkinter.Button(self.root, text = '开始/停止', command = self.newtask,bg='gold')
        self.btn_start.place(x=200, y=165, width=100, height=50)

        self.btn1 = tkinter.Button(self.root, text='滚动名单展示', bg='white')
        self.btn1.config(font=("Courier", 20))
        self.btn1.place(x=300, y=165, width=900, height=80)

        # 以一等奖为例，此处代码只是示例，参数不准确
        # 添加一等奖的按钮组件 Button
        self.first = tkinter.Button(self.root, text='一等奖名单', command=self.set_first, bg='gold')
        # 将该按钮置于界面相应位置
        self.first.place(x=600, y=300, width=100, height=50)
        # 设置获奖榜展示字体
        myFont = Font(name='Times New Roman',size=20)
        # 添加一等奖的获奖名单展示组件 Listbox
        self.target_1 = tkinter.Listbox(self.root, height=5, bg="lemonchiffon", bd=0, fg="olive")
        self.target_1.pack()
        # 将展示组件添加到界面中
        self.target_1.place(x=600, y=360, width=150, height=600)

        # 添加二等奖的按钮组件 Button
        self.second = tkinter.Button(self.root, text='二等奖名单', command=self.set_second, bg='gold')
        # 将该按钮置于界面相应位置
        self.second.place(x=800, y=300, width=100, height=50)
        # 设置获奖榜展示字体
        myFont = Font(name='Times New Roman', size=20)
        # 添加一等奖的获奖名单展示组件 Listbox
        self.target_2 = tkinter.Listbox(self.root, height=5, bg="lemonchiffon", bd=0, fg="olive")
        # 将展示组件添加到界面中
        self.target_2.place(x=800, y=360, width=150, height=600)

        # 添加三等奖的按钮组件 Button
        self.third = tkinter.Button(self.root, text='三等奖名单', command=self.set_third, bg='gold')
        # 将该按钮置于界面相应位置
        self.third.place(x=1000, y=300, width=100, height=50)
        # 设置获奖榜展示字体
        myFont = Font(name='Times New Roman', size=20)
        # 添加一等奖的获奖名单展示组件 Listbox
        self.target_3 = tkinter.Listbox(self.root, height=5, bg="lemonchiffon", bd=0, fg="olive")
        # 将展示组件添加到界面中
        self.target_3.place(x=1000, y=360, width=150, height=600)
        self.var = StringVar()
        self.noteLable = Label(text="请输入抽奖人数：")
        self.noteLable.place(anchor=NW, x=110, y=30)
        self.input = Entry(self.root, show=None)
        self.input.place(anchor=NW, x=230, y=30)



        resultLable = Label(textvariable= self.var)
        resultLable.place(anchor=NW, x=150, y=100)

        # 总名单展示区域选用 Text 组件
        self.source = tkinter.Text(self.root, bg="navajowhite", fg="dimgray")
        # 将该组件添加到界面中
        self.source.place(x=100, y=250, width=300, height=290)
        # 向组件中插入数据
        # self.source.insert(tkinter.END, ("、").join(self.data))
        # 重启按钮 Button
        self.btn_reset = tkinter.Button(self.root, text='重启', command=self.reset, bg='gold')
        # 重启按钮添加到界面中
        self.btn_reset.place(x=180, y=550, width=100, height=50)


    def rounds(self):
        self.value = self.input.get()
        if(self.value):
            self.value = self.input.get()
        else:
            self.value = 3

        print("value:",self.value)
        # 判断是否开始循环
        # if self.isloop == True:
        #     return
        # 初始化计数 变量
        i = 0
        # 死循环
        while True:
            if self.newloop == True:
                self.newloop = False
                for item in r:
                    self.target_1.insert("end", item)
                    total_na.remove(item)
                print("total_n!!!:", total_na)
                return
            # 延时操作
            time.sleep(0.1)
            # 将所有的组件背景变为白色
            r = random.sample(total_na,int(self.value))
            print("r:",r)
            print("total_n:",total_na)
            self.btn1['text'] = r

    def rounds_2(self):
        self.value = self.input.get()
        if(self.value):
            self.value = self.input.get()
        else:
            self.value = 3
        # if self.isloop == True:
        #     return
        # 初始化计数 变量
        i = 0
        # 死循环
        while True:
            if self.newloop == True:
                self.newloop = False
                for item in r:
                    self.target_2.insert("end", item)
                    total_na.remove(item)
                return
            # 延时操作
            time.sleep(0.1)
            # 将所有的组件背景变为白色
            r = random.sample(total_na,int(self.value))
            print("r:",r)
            self.btn1['text'] = r

    def rounds_3(self):
        self.value = self.input.get()
        if(self.value):
            self.value = self.input.get()
        else:
            self.value = 3
        # 判断是否开始循环
        # if self.isloop == True:
        #     return
        # 初始化计数 变量
        i = 0
        # 死循环
        while True:
            if self.newloop == True:
                self.newloop = False
                for item in r:
                    self.target_3.insert("end", item)
                    total_na.remove(item)
                return
            # 延时操作
            time.sleep(0.1)
            # 将所有的组件背景变为白色
            r = random.sample(total_na,int(self.value))
            print("r:",r)
            self.btn1['text'] = r

    # 建立一个新线程的函数
    def newtask(self):
        if self.isloop == False:
            # 建立线程
            t = threading.Thread(target = self.rounds)
            # 开启线程运行
            t.start()
            # 设置循环开始标志ask(self):nknewtas
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True

    def reset(self):
        self.target_1.delete(0,"end")
        self.target_2.delete(0, "end")
        self.target_3.delete(0, "end")
        print("reset data")





c = Choujiang()